<?php
class Place extends Vfriend_Db {	
	var $cache;
	var $clear;
	public function __construct() {
		$this->cache 		= new Vfriend_Cache();
		$this->clear 		= $this->cache->clear;	
	}	
	public function get($intPlaceId) {
		try {
			$strKey 	= PLACE_GET . $intPlaceId;
			$arrResult	= $this->cache->get($strKey);			
			if(!Vfriend_Function::isCached($arrResult)) {
				$this->prepare('call places_get(?)');
				$this->bindParam(1, $intPlaceId, PDO::PARAM_INT, 11);
				$arrResult = $this->fetchAll();
				$this->cache->set($strKey, $arrResult, PLACE_GET_EXPIRE);
			}			
			return isset($arrResult[0]) ? $arrResult[0] : null;
		} catch(Exeception $e) {
			Vfriend_Function::logDb('Backend::Place::get - ' .$e->getMessage());			
			return null;
		}
	}
	public function getAll($intStatus, $intProvince, $intOption, $strKey, $strStart, $strEnd, $intOff, $intLimit, &$intTotal) {
		try {						
			$this->prepare('call places_getall_admin(?,?,?,?,?,?,?,?)');
			$this->bindParam(1, $intStatus, PDO::PARAM_INT, 4);
			$this->bindParam(2, $intProvince, PDO::PARAM_INT, 11);
			$this->bindParam(3, $intOption, PDO::PARAM_INT, 4);
			$this->bindParam(4, $strKey, PDO::PARAM_STR, 50);
			$this->bindParam(5, $strStart, PDO::PARAM_STR, 10);
			$this->bindParam(6, $strEnd, PDO::PARAM_STR, 10);
			$this->bindParam(7, $intOff, PDO::PARAM_INT, 4);
			$this->bindParam(8, $intLimit, PDO::PARAM_INT, 4);
			$arrResult 	= $this->fetchAll();
			$intTotal	= isset($arrResult[0]['foundRows']) ? $arrResult[0]['foundRows'] : 0;
			return isset($arrResult) ? $arrResult : null;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Backend::Place::getAll - ' .$e->getMessage());			
			return null;
		}
	}	
	public function getImages($intPlaceId, $intOffset=0, $intLimit=PLACE_IMG_LIMIT, &$intTotal=0)
	{
		try {		
			$arrResult	= null;
			if($intOffset == 0) {
				$strKey 	= PLACE_GETIMAGES . $intPlaceId . '1';
				$arrResult	= $this->cache->get($strKey);
			}
			if(!Vfriend_Function::isCached($arrResult)) {
				$intLimit	= $intLimit * 2;
				$this->prepare('call places_img_getall(?,?,?,?)');
				$this->bindParam(1, $intType=1, PDO::PARAM_INT, 4);	
				$this->bindParam(2, $intPlaceId, PDO::PARAM_INT, 11);	
				$this->bindParam(3, $intOffset, PDO::PARAM_INT, 11);	
				$this->bindParam(4, $intLimit, PDO::PARAM_INT, 4);	
				$arrResult 	= $this->fetchAll();	
				if($intOffset == 0) {
					$this->cache->set($strKey,$arrResult,PLACE_GETIMAGES_EXPIRE);
				}
			}
			$intTotal = isset($arrResult[0]['foundRows']) ? $arrResult[0]['foundRows'] : 0;			
			return isset($arrResult) ? $arrResult : null;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Backend::Place::getImages - ' .$e->getMessage());			
			return null;
		}
	}	
	public function updateStatus($intAdminId, $intPlaceId, $intStatus, $intPerm, $intUserId=0) {
		try {				
			$this->prepare('call places_update_status(?,?,?,?)');
			$this->bindParam(1, $intAdminId, PDO::PARAM_INT, 11);
			$this->bindParam(2, $intPlaceId, PDO::PARAM_INT, 11);
			$this->bindParam(3, $intStatus, PDO::PARAM_INT, 4);
			$this->bindParam(4, $intPerm, PDO::PARAM_INT, 4);
			$arrResult 	= $this->fetchAll();
			$intError	= isset($arrResult[0]['errCode']) ? $arrResult[0]['errCode'] : 0;
			if($intError == 0) {
				$this->clear->deletePlaceById($intPlaceId);
				$this->clear->deletePlaceFavourite($intUserId);
				$this->clear->deletePlaceForCmt($intPlaceId, $intUserId);
			}
			return $intError;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Backend::Place::updateStatus - ' .$e->getMessage());				
			return null;
		}
	}
	public function delete($intAdminId, $intPlaceId, $intUserId=0) {
		try {				
			$this->prepare('call places_delete(?,?)');
			$this->bindParam(1, $intAdminId, PDO::PARAM_INT, 11);
			$this->bindParam(2, $intPlaceId, PDO::PARAM_INT, 11);
			$arrResult 	= $this->fetchAll();
			$intError	= isset($arrResult[0]['errCode']) ? $arrResult[0]['errCode'] : 0;
			if($intError == 0) {
				$strImage	= isset($arrResult[0]['image']) ? $arrResult[0]['image'] : '';
				if($strImage != '')
					$this->deleteImg($strImage);
				$arrImage	= $this->getImages($intPlaceId);
				if($arrImage) {
					foreach($arrImage as $value) {
						$this->deleteImageForPlace($intAdminId, $intPlaceId, $value['pimageid']);
					}
				}
				$this->clear->deletePlaceById($intPlaceId);
				$this->clear->deletePlaceFavourite($intUserId);
				$this->clear->deletePlaceForCmt($intPlaceId, $intUserId);
			}
			return $intError;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Backend::Place::delete - ' .$e->getMessage());				
			return null;
		}
	}
	public function updateStatusForImg($intAdminId, $intPimageId, $intStatus, $intPerm, $intPlaceId=0, $intUserId=0) {
		try {				
			$this->prepare('call places_img_update_status(?,?,?,?)');
			$this->bindParam(1, $intAdminId, PDO::PARAM_INT, 11);
			$this->bindParam(2, $intPimageId, PDO::PARAM_INT, 11);
			$this->bindParam(3, $intStatus, PDO::PARAM_INT, 4);
			$this->bindParam(4, $intPerm, PDO::PARAM_INT, 4);
			$arrResult 	= $this->fetchAll();
			$intError	= isset($arrResult[0]['errCode']) ? $arrResult[0]['errCode'] : 0;
			if($intError == 0) {				
				$this->clear->deletePlaceById($intPlaceId);
				$this->clear->deletePlaceFavourite($intUserId);
				$this->clear->deletePlaceForCmt($intPlaceId, $intUserId);
				$this->clear->deletePlaceForImg($intPimageId);
			}
			return $intError;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Backend::Place::updateStatusForImg - ' .$e->getMessage());				
			return null;
		}
	}
	public function deleteImageForPlace($intAdminId, $intPlaceId, $intPimageId) {
		try {			
			$this->prepare('call places_img_delete_admin(?,?,?)');
			$this->bindParam(1, $intAdminId, PDO::PARAM_INT, 11);	
			$this->bindParam(2, $intPlaceId, PDO::PARAM_INT, 11);	
			$this->bindParam(3, $intPimageId, PDO::PARAM_INT, 11);	
			$arrResult 	= $this->fetchAll();	
			$intError	= isset($arrResult[0]['errCode']) ? $arrResult[0]['errCode'] : 0;
			if($intError == 0) {	
				$strImage	= isset($arrResult[0]['image']) ? $arrResult[0]['image'] : '';
				if($strImage != '') {
					$this->deleteImg($strImage);
				}
				$this->clear->deletePlaceForImg($intPimageId);
				$this->clear->deletePlaceById($intPlaceId);
			}
			return $intError;
		} catch(Exception $e) {			
			Vfriend_Function::logDb('Backend::Place::deleteImage - ' .$e->getMessage());			
			return null;
		}
	}
	public function deleteImg($strImg) {		
		if(file_exists(DIR_PLACE_FULL . $strImg))
			@unlink(DIR_PLACE_FULL . $strImg);
		if(file_exists(DIR_PLACE_THUMB . $strImg))
			@unlink(DIR_PLACE_THUMB . $strImg);
		if(file_exists(DIR_PLACE_SMALL . $strImg))
			@unlink(DIR_PLACE_SMALL . $strImg);
	}
}
?>
